package com.hua.demo.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hua.R;
import com.hua.demo.edu.client.myown;
import com.hua.demo.edu.entity.Teacher;
import com.hua.demo.edu.entity.vo.TeacherQuery;
import com.hua.demo.edu.mapper.TeacherMapper;
import com.hua.demo.edu.service.TeacherService;
import com.hua.demo.edu.service.impl.TeacherServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-11-10
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/edu/teacher")
//@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherServiceImpl teacherService;
    @Autowired
    private myown myown;

    @GetMapping("findall")
    private R all(){
        List<Teacher> list = teacherService.list(null);
//        Teacher byId = teacherService.getById("1");
        return R.ok().data("items",list);
    }
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/delete/{id}")
    public R removeteacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id")String id){
        boolean flag = teacherService.removeById(id);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页查询讲师的方法
    @PostMapping("/pageteacher/{current}/{size}")
    public R pagelistteacher(@PathVariable("current") long current,@PathVariable("size") long size){
        Page<Teacher> pageteacher= new Page<Teacher>(current, size);
        //调用方法实现分页
       teacherService.page(pageteacher, null);
        long total= pageteacher.getSize();
        System.out.println(total);
        List<Teacher> records = pageteacher.getRecords();//数据list集合

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",records);

    }


    //4 条件查询带分页的方法
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<Teacher> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //调用方法实现条件查询分页
        IPage<Teacher> page = teacherService.page(pageTeacher, wrapper);
//        int total = pageTeacher.getRecords().size();
        long total = pageTeacher.getTotal();
        System.out.println(total);
        List<Teacher> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("/addteacher")
    public R add(@RequestBody Teacher t){
        boolean save = teacherService.save(t);
        if(save){
            return R.ok();
        }else
            return R.error();
    }
    @PostMapping("/update")
    public R update(@RequestBody Teacher t){

        boolean update = teacherService.updateById(t);
        if(update){
            return R.ok();
        }else
            return R.error();
    }

    @GetMapping("/getone/{id}")
    public R getone(@PathVariable String id){
        Teacher byId = teacherService.getById(id);
        return R.ok().data("teacher",byId);


    }

    @GetMapping("fuwudiaoyong")
    public String get(){
        String getall = myown.getall();
        return getall;
    }






}

